package com.ygo.feihua.Management;
import java.net.URL;
import java.net.MalformedURLException;
import java.net.HttpURLConnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;
import com.ygo.feihua.type.RobotLiaotian;
import org.json.JSONObject;
import org.json.JSONException;
import com.ygo.feihua.adapter.AdapterRobot;
import android.os.Handler;
import android.os.Message;
import com.ygo.feihua.RobotFeihua;

public class RobotManagement
{
	public static final String APP_KEY="8a104d98dad34ae5bcddcbf73138ed82";
	public static final String URL="http://www.tuling123.com/openapi/api";

	private static InputStream is;
	static AdapterRobot adpro;
	static RobotFeihua roff;
	private static ByteArrayOutputStream baos;

	static Handler han=new Handler(){

		@Override
		public void handleMessage(Message msg)
		{
			// TODO: Implement this method
			super.handleMessage(msg);
			
			switch(msg.what){
				case 0:
					adpro.sx((RobotLiaotian)msg.obj);
					roff.sx();
					break;
			}
		}
	};
	
	
	public static RobotLiaotian getMessage (final RobotFeihua rof, final AdapterRobot adprt, final String msg){
		final RobotLiaotian rtl=new RobotLiaotian();
		rtl.setName("小废");
		rtl.setType(0);
		new Thread(new Runnable(){

				@Override
				public void run()
				{
					String jsonRes=doGet(msg);
					try
					{
						JSONObject jso=new JSONObject(jsonRes);
						rtl.setMsg(jso.getString("text"));
						switch(jso.getInt("code")){
							case 100000:

								break;
							case 200000:
								rtl.setMsg(jso.getString("text")+"\n"+jso.getString("url"));
								break;
						}
					}
					catch (JSONException e)
					{
						rtl.setMsg("服务器繁忙,请稍候再试");
					}

					Message me=new Message();
					adpro = adprt;
					roff = rof;
					me.what = 0;
					me.obj = rtl;
					han.sendMessage(me);

					// TODO: Implement this method
				}
			}).start();
		
		return rtl;
	}
	
	public static String doGet(String msg){
		String result="";
		
		String url=setParams(msg);
		
		try
		{
			URL urlNet=new URL(url);
			
				HttpURLConnection conn=(HttpURLConnection) urlNet.openConnection();
				
				conn.setReadTimeout(5*1000);
				conn.setConnectTimeout(5*1000);
				conn.setRequestMethod("GET");
				 is=conn.getInputStream();
				int len=-1;
				byte[] buf=new byte[128];
				 baos=new ByteArrayOutputStream();
				while((len=is.read(buf))!=-1){
					
					baos.write(buf,0,len);
				}
				baos.flush();
				result=new String(baos.toByteArray());
			}
			
		catch (Exception e)
		{}finally{
			try
			{
				if (is != null)
					is.close();
			}catch (IOException e){}
			try
			{
				if (baos != null)
					baos.close();
			}
			catch (IOException e)
			{}

		}
		return result;
	}

	private static String setParams(String msg)
	{
		String url="";
		try
		{
			 url=URL + "?key=" + APP_KEY + "&info=" + URLEncoder.encode(msg, "UTF-8");
		}catch (UnsupportedEncodingException e){
			
		}

		// TODO: Implement this method
		return url;
	}
	
	
}
